package com.pureut.equipment.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.equipment.domain.dto.MaintenanceWorkOrderDto;
import com.pureut.equipment.domain.vo.AuditVo;
import com.pureut.equipment.domain.vo.MaintenanceWorkOrderVo;
import com.pureut.equipment.service.MaintenanceWorkOrderService;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/***
 *
 * Author:保养工单控制层
 * Date:2023/04/24 9:51
 * @DESC
 */
@RestController
@RequestMapping("/maintenance-work-order")
public class MaintenanceWorkOrderController extends BaseController {

    /**
     * 保养工单service
     */
    @Resource
    MaintenanceWorkOrderService maintenanceWorkOrderService;

    /**
     * 保养工单列表
     */
    @SaCheckPermission("device:maintenance:workorder:list")
    @GetMapping("/list")
    public TableDataInfo<MaintenanceWorkOrderDto> list(MaintenanceWorkOrderVo maintenanceWorkOrderVo, PageQuery pageQuery) {
        return maintenanceWorkOrderService.getList(maintenanceWorkOrderVo,pageQuery);
    }

    /**
     * 保养工单新增
     */
    @SaCheckPermission("device:maintenance:workorder:add")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody MaintenanceWorkOrderVo maintenanceWorkOrderVo) throws Exception {
        return toAjax(maintenanceWorkOrderService.add(maintenanceWorkOrderVo));
    }

    /**
     * 保养工单新增(当日生成调用)
     */
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/addToday")
    public R<Void> addToday(@RequestBody MaintenanceWorkOrderVo maintenanceWorkOrderVo) throws Exception {
        return toAjax(maintenanceWorkOrderService.addToday(maintenanceWorkOrderVo));
    }

    /**
     * 保养工单新增(定时任务调用)
     */
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/addScheduling")
    public R<Void> addScheduling(@RequestBody MaintenanceWorkOrderVo maintenanceWorkOrderVo) throws Exception {
        return toAjax(maintenanceWorkOrderService.addScheduling(maintenanceWorkOrderVo));
    }

    /**
     * 保养工单派工
     */
    @SaCheckPermission("device:maintenance:workorder:dispatch")
    @Log(title = "保养工单派工", businessType = BusinessType.UPDATE)
    @PostMapping("/dispatch")
    public R<Void> dispatch(@RequestBody MaintenanceWorkOrderVo maintenanceWorkOrderVo) {
        return toAjax(maintenanceWorkOrderService.dispatch(maintenanceWorkOrderVo));
    }

    /**
     * 执行保养
     */
    @SaCheckPermission("device:maintenance:workorder:maintenance")
    @Log(title = "执行保养", businessType = BusinessType.UPDATE)
    @PostMapping("/execute")
    public R<Void> execute(@RequestBody MaintenanceWorkOrderVo maintenanceWorkOrderVo) {
        return toAjax(maintenanceWorkOrderService.execute(maintenanceWorkOrderVo));
    }

    /**
     * 提交审核
     */
    @SaCheckPermission("maintenance:work:order:audit")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/audit/{id}")
    public R<Void> audit(@PathVariable("id") Long id) throws Exception {
        return toAjax(maintenanceWorkOrderService.audit(id));
    }

    /**
     * 撤销审核
     */
    @SaCheckPermission("device:maintenance:workorder:revoke")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/revocation/{id}")
    public R<Void> revocation(@PathVariable("id") Long id) throws Exception {
        return toAjax(maintenanceWorkOrderService.revocation(id));
    }

    /**
     * 单据审核
     */
    @SaCheckPermission("maintenance:workorder:examine")
    @Log(title = "单据审核", businessType = BusinessType.UPDATE)
    @PostMapping("/documentAudit")
    public R<Void> documentAudit(@RequestBody AuditVo auditVo) throws Exception {
        return toAjax(maintenanceWorkOrderService.documentAudit(auditVo));
    }

    /**
     * 修改
     */
    @SaCheckPermission("device:maintenance:workorder:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateWorkOrder")
    public R<Void> updateWorkOrder(@RequestBody MaintenanceWorkOrderVo maintenanceWorkOrderVo) throws Exception {
        return toAjax(maintenanceWorkOrderService.updateWorkOrder(maintenanceWorkOrderVo));
    }

    /**
     * 查看
     */
    @SaCheckPermission("device:maintenance:workorder:query")
    @GetMapping("/getView/{id}")
    public R<MaintenanceWorkOrderDto> getView(@PathVariable("id") Long id) throws Exception {
        return R.ok(maintenanceWorkOrderService.getView(id));
    }

    /**
     * 删除
     */
    @SaCheckPermission("device:maintenance:workorder:remove")
    @DeleteMapping("/deleteWorkOrder/{id}")
    public R<Void> deleteWorkOrder(@PathVariable("id") Long id) {
        return toAjax(maintenanceWorkOrderService.deleteWorkOrder(id));
    }

}
